package com.bw.shopping.adapter;

import android.util.SparseIntArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.bw.shopping.R;
import com.bw.shopping.entity.Shopcarentity;
import com.bw.shopping.mvvm.viewmodel.ShopViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

public
/**
 * @author Lishaohe
 * @description:
 * @date :2021/10/22 0022 上午 10:26
 *
 */
class ShopCarAdapter extends BaseQuickAdapter<Shopcarentity.DataBean, BaseViewHolder>  {

    private shopMoney shopMoney;

    public ShopCarAdapter.shopMoney getShopMoney() {
        return shopMoney;
    }

    public void setShopMoney(ShopCarAdapter.shopMoney shopMoney) {
        this.shopMoney = shopMoney;
    }

    public ShopCarAdapter() {
        super(R.layout.item_car);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, Shopcarentity.DataBean dataBean) {
        Glide.with(getContext()).load(dataBean.getGoods_icon()).into((ImageView) baseViewHolder.getView(R.id.car_item_img));
        baseViewHolder.setText(R.id.car_item_title,dataBean.getGoods_desc());
        baseViewHolder.setText(R.id.car_item_dec,dataBean.getGoods_sku());
        baseViewHolder.setText(R.id.car_item_price,dataBean.getGoods_price());
        baseViewHolder.setText(R.id.car_item_num,dataBean.getGoods_count()+"");

        //数量
        TextView count = baseViewHolder.itemView.findViewById(R.id.car_item_num);
        CheckBox checkBox = baseViewHolder.itemView.findViewById(R.id.car_item_radio);
        if (dataBean.getFlag()){
            checkBox.setChecked(true);
        }else {
            checkBox.setChecked(false);
        }

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dataBean.getFlag()){
                    dataBean.setFlag(false);
                    checkBox.setChecked(false);
                    shopMoney.shopMoney(false, dataBean.getGoods_count(), baseViewHolder.getLayoutPosition());
                }else {
                    dataBean.setFlag(true);
                    checkBox.setChecked(true);
                    shopMoney.shopMoney(true, dataBean.getGoods_count(), baseViewHolder.getLayoutPosition());
                }
            }
        });





        //-
        TextView left = baseViewHolder.itemView.findViewById(R.id.car_item_left);
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (dataBean.getGoods_count()<1||count.getText().equals("1")){
                    ToastUtils.showLong("最少1件");
                }
                dataBean.setGoods_count(dataBean.getGoods_count()-1);
                if (dataBean.getFlag()){
                    shopMoney.shopMoney(dataBean.getFlag(), dataBean.getGoods_count(), baseViewHolder.getLayoutPosition());
                }
                count.setText(""+dataBean.getGoods_count());
            }
        });
        //+
        TextView right = baseViewHolder.itemView.findViewById(R.id.car_item_right);
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBean.setGoods_count(dataBean.getGoods_count()+1);
                if (dataBean.getFlag()){
                    shopMoney.shopMoney(true, dataBean.getGoods_count(), baseViewHolder.getLayoutPosition());
                }
                count.setText(""+dataBean.getGoods_count());
            }
        });



    }
    public interface shopMoney{
        void shopMoney(Boolean flag,Integer num,Integer position);
    }

}
